﻿// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<string>
#include<curl>
#include<map>
#include<regex>
#include<sstream>
#include<filesystem>
#include<fstream>
using namespace std;
vector<string> URL_VT = vector<string>();
const string URL = "";
const static string PATH = "./movies";
const static int PAGE = 10;

/*处理http请求返回数据的回调函数*/
size_t writeCallback(void)

string request_get(const string& url, map<string, string>headers = map<string, string>()) {
    CURL* curl;
    CURLcode statusCode;
    string htmlData;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl,)
    }
}
bool contains(vector<string>& vt, const string& value) {
    return find(vt.begin(), vt.end(), value) != vt.end();
}
void getURL(const string& htmlData) {
    string url;
    string html;
    smatch result;
    regex reg("/detail/\\d+");
    for (int page = 10;page <= PAGE;++page) {
     url=URL + "/page/" + to_string(page);
        html = request_get(url);
        auto end_it = sregex_iterator();
        sregex_iterator it(html.begin(), html.end(), reg);
        for (;it != end_it;++it) {
            result = *it;
            cout << result.str() << endl;
            if (!contains(URL_VT, result.str())) {
                URL_VT.push_back(result.str());
            }
        }
    }
}
/**/
vector<string> findAll(const string& html, regex reg,int group=0) {
    auto end_it = sregex_iterator();
    vector<string> vt;
    smatch result;
    sregex_iterator it(html.begin(), html.end(), reg);
    for (;it != end_it;++it) {
        result = *it;
       // cout << result[group].str() << endl;
        vt.push_back(result[group].str());
    }
    return vt;
}/*将vector中的字符串连接在一起*/
string vecTostring(vector<string>& vt) {
    ostringstream ostr;
    for (auto& w : vt) {
        ostr << w << " ";
    }
    return ostr.str();
}
/*返回一个保存电影信息的map函数*/
map<string,string> parseDetailPage(const string& url) {
    string html = request_get(url);
    smatch ResultTitle, ResultCategories,ResultArea, ResultTime, ResultPublic, ResultDrama, ResultScore;
    map<string, string> ResultMap;

    regex RegTitle(">(.*)</h2>");
    regex _search(html, ResultTitle, RegTitle);
    cout << ResultTitle[1].str() << endl;

    regex RegCategories("<span>(.*)</span>");
    vector<string>vt = findAll(html, RegCategories, 1);
    vt.pop_back();


    regex RegArea("\">([^00-xff^/J+)</span>");
    regex _search(html, ResultArea, RegArea);
    cout << ResultArea[1].str() << endl;

    regex RegTime(">(\\d+).*</span>");
    regex _search(html, ResultTime, RegTime);
    cout << ResultTime[1].str() << endl;

    regex RegPublic(">(\\d+-\\d+-\\d+)");
    regex _search(html, ResultPublic, RegPublic);
    cout << ResultPublic[1].str() << endl;

    regex RegDrama(">\\s+(.*)\\s+</p>");
    regex _search(html, ResultDrama, RegDrama);
    cout << ResultDrama[1].str() << endl;

    regex RegScore("\\d+\\.\\d+)");
    regex _search(html, ResultScore, RegScore);
    cout << ResultScore[1].str() << endl;

    ResultMap["title"] = ResultTitle[1].str();
    ResultMap["categories"] = vecTostring(vt);
    ResultMap["arez"] = ResultArea[1].str();
    ResultMap["time"] = ResultTime[1].str();
    ResultMap["public"] = ResultPublic[1].str();
    ResultMap["score"] = ResultScore.str();
    return ResultMap;

}
string Conver
int main()
{
    SetConsoleOutputCP(65001);
    if (!filesystem::exists(PATH)) {
        filesystem::creat_directory(PATH);

    }
    //string html=request_get("http:/ssrl.scrape.center/page/2");
    //getURL(html);
    string url;
    map<string, string>resultMap;
    string fileName;
    while (!URL_VT.empty()) {
        url = URL_VT.back();
        URL_VT.pop_back();

        resultMap = parseDetailPage(url);
        fileName = PATH + "/" + resultMap["title"] + ".txt";
        fstream fout(fileName,ios::out);
        for (auto& m : resultMap) {
            fout << m.first << " " << m.second << endl;
        }
    }
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
